/**
' * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *MeetPersonRecordController
 * 会议人员记录
 *
 * 修改纪录
 * 2020-10-31 版本：1.0 donghang 创建。
 * @version 版本：1.0
 * @author 作者：donghang
 * 创建日期：2020-10-31
 */

package com.topscomm.meet.controller;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.meet.controller.auto.MeetPersonRecordControllerAuto;
import com.topscomm.meet.pojo.MeetPersonRecordEntity;
import com.topscomm.meet.pojo.MeetRecordEntity;
import com.topscomm.meet.service.IMeetRecordService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: donghang
 * @date: 2020-10-31
 * @description:会议人员记录Controller
 * @modify:
 */
@RestController
@RequestMapping("meet/personRecord")
public class MeetPersonRecordController extends MeetPersonRecordControllerAuto implements IMeetPersonRecordController {

	@Autowired
	private IMeetRecordService meetRecordService;
	/**
	 * @author: donghang
	 * @date: 2020-10-31
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_meetid")) {
			String value = paraMap.get("qry_meetid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and meetid = '{0}'", value);
		}
		if (paraMap.containsKey("qry_submitstate")) {
			String value = paraMap.get("qry_submitstate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and submitstate = {0}", value);
		}
		if (paraMap.containsKey("qry_signstate")) {
			String value = paraMap.get("qry_signstate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and signstate = {0}", value);
		}
		return sql.toString();
	}

	/**
	 * @author: donghang
	 * @date: 2020-10-31
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description: 签到
	 * @param  meeid
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-11-4 15:21:03
	 * @modify:
	 */
	@Override
	@PostMapping(value = "/sign")
	public ResponseResult<Object> sign(@RequestBody Map<String,Object> paraMap) {
		String meetid = ConvertUtil.convertToString(paraMap.get(MeetPersonRecordEntity.FieldMeetid));
		String replaceusercode = ConvertUtil.convertToString(paraMap.get("replaceusercode"));
		String replaceusername= ConvertUtil.convertToString(paraMap.get("replaceusername"));
		this.service.sign(meetid,replaceusercode,replaceusername);
		return ResponseResult.ok("签到成功！");
	}

	/**
	 * @description: 邀请人员参会
	 * @param meetid 会议id
	 * @param usercode 多个用户code
	 * @param username 多个用户name
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-11-5 15:20:03
	 * @modify:
	 */
	@Override
	@PostMapping(value = "/invite")
	public ResponseResult<Object> invite(@RequestBody Map<String,Object> paraMap) {
		try {
			String meetid = ConvertUtil.convertToString(paraMap.get(MeetPersonRecordEntity.FieldMeetid));
			String usercode = ConvertUtil.convertToString(paraMap.get(MeetPersonRecordEntity.FieldUsercode));
			String username = ConvertUtil.convertToString(paraMap.get(MeetPersonRecordEntity.FieldUsername));
			String message = this.service.invite(meetid,usercode,username );
			if (StringUtil.isEmpty(message)) {
				message = "邀请成功！";
			}
			return ResponseResult.ok(message);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @description: 指派人员参会
	 * @param usercode 多个用户code
	 * @param username 多个用户name
	 * @param meetid 会议id
	 * @param usercodes
	 * @param usernames
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-11-5 15:22:35
	 * @modify:
	 */
	@Override
	@PostMapping(value = "/assign")
	public ResponseResult<Object> assign(@RequestBody  Map<String,Object> paraMap) {
		try {
			String meetid = ConvertUtil.convertToString(paraMap.get(MeetPersonRecordEntity.FieldMeetid));
			String usercodes = ConvertUtil.convertToString(paraMap.get(MeetPersonRecordEntity.FieldUsercode));
			String usernames = ConvertUtil.convertToString(paraMap.get(MeetPersonRecordEntity.FieldUsername));
			String message = this.service.assign(meetid,usercodes,usernames);
			if (StringUtil.isEmpty(message)) {
				message = "指派成功！";
			}
			return ResponseResult.ok(message);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @description: 报名参会
	 * @param paraMap
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-11-5 15:23:04
	 * @modify:
	 */
	@Override
	@PostMapping(value = "/join")
	public ResponseResult<Object> join(@RequestBody(required = false) Map<String, Object> paraMap) {
		this.service.join(paraMap);
		String message = "报名成功！";
		return ResponseResult.ok(message);
	}

	/**
	 * @description: 查询参会人员统计数目
	 * @param meetid
	 * @return
	 * @author: wanghongwei
	 * @date: 2020-11-9 10:44:07
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/queryPersonStaticCount")
	public ResponseResult<Object> queryPersonStaticCount(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> staticMap = this.service.getPersonStaticCount(paraMap);
			return ResponseResult.ok(staticMap);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @description:查询列表
	 * @param paraMap
	 * @return
	 * @author: donghang
	 * @date: 2019年8月8日下午6:06:52
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String sidx = ConvertUtil.convertToString(paraMap.get("sidx"));
			String usercode = MeetPersonRecordEntity.FieldUsercode;
			// 当按照员工工号排序时，先按照工号长度排序
			if (!StringUtil.isEmpty(sidx) && usercode.equals(sidx.replace(" ", ""))) {
				paraMap.put("sidx", SQLAdapter.len(usercode) + " asc, " + usercode);
			}
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			Page page = new Page(new ArrayList<>(), 0, query.getPageSize(), query.getCurrentPage());
			// 补录会议纪要查询参会人员，不存在会议
			if (paraMap.containsKey("supplement")) {
				MeetRecordEntity record = this.meetRecordService.queryById(ConvertUtil.convertToString(paraMap.get("qry_recordid")));
				if (!StringUtil.isEmpty(record.getAbsentusercodes()) || !StringUtil.isEmpty(record.getAttendusercodes())) {
					String usercodes = (StringUtil.isEmpty(record.getAttendusercodes()) ? "" : record.getAttendusercodes())
							+ "," + (StringUtil.isEmpty(record.getAbsentusercodes()) ? "" : record.getAbsentusercodes());
					String[] split = usercodes.split(",");
					List<String> usercodeList = Arrays.stream(split).filter(item -> !StringUtil.isEmpty(item))
							.distinct().collect(Collectors.toList());
					usercodes = usercodeList.stream().collect(Collectors.joining("','", "'", "'"));
					Map<String, Object> param = new HashMap<>();
					param.put("usercodes", usercodes);
					param.put("begincount", query.getCurrentPage() * query.getPageSize() - query.getPageSize());
					param.put("pagesize", query.getPageSize());
					List<Map<String, Object>> tempList = null;
					if (query.getPageSize() > 0) {
						tempList = this.service.queryMap(param, "queryUserInfoByCodeForPage");
					} else {
						tempList = this.service.queryMap(param, "queryUserInfoByCode");
					}
					page = new Page(tempList, usercodeList.size(), query.getPageSize(), query.getCurrentPage());
				}
			} else { // 正常会议参会人员
				int totalCount = this.service.getCount(sqlWhere);
				List<Map<String, Object>> tempList = null;
				if (query.getPageSize() > 0) {
					tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
							query.getSidx(), query.getSord());
				} else {
					tempList = this.service.queryMapByWhere(sqlWhere);
				}
				page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			}
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

}
